package com.example.bookappgraduation.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.bookappgraduation.BookDetailActivity;
import com.example.bookappgraduation.LoginActivity;
import com.example.bookappgraduation.R;
import com.example.bookappgraduation.adapters.BookAdapter;
import com.example.bookappgraduation.models.Book;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class FavoritesFragment extends Fragment {

    private RecyclerView recyclerViewFavorites;
    private BookAdapter favoriteBookAdapter;
    private List<Book> favoriteBookList;
    private TextView textViewNoFavorites;
    private SharedPreferences sharedPreferences;

    public FavoritesFragment() { }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getContext() != null) {
            sharedPreferences = getContext().getSharedPreferences(LoginActivity.PREFS_NAME, Context.MODE_PRIVATE);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_favorites, container, false);

        recyclerViewFavorites = view.findViewById(R.id.recyclerViewFavorites);
        textViewNoFavorites = view.findViewById(R.id.textViewNoFavorites);
        recyclerViewFavorites.setLayoutManager(new LinearLayoutManager(getContext()));//让列表垂直排列

        favoriteBookList = new ArrayList<>();
        favoriteBookAdapter = new BookAdapter(getContext(), favoriteBookList); // 复用 BookAdapter
        recyclerViewFavorites.setAdapter(favoriteBookAdapter);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadFavoriteBooks(); // 每次 Fragment 可见时重新加载，确保数据最新
    }

    private void loadFavoriteBooks() {
        if (sharedPreferences == null && getContext() != null) { // 再次检查
            sharedPreferences = getContext().getSharedPreferences(LoginActivity.PREFS_NAME, Context.MODE_PRIVATE);
        }
        if (sharedPreferences == null) return; // 如果还是 null，则无法加载

        String jsonFavorites = sharedPreferences.getString(BookDetailActivity.FAVORITES_PREF_KEY, null);
        favoriteBookList.clear(); // 清空现有列表

        if (jsonFavorites != null) {
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<Book>>() {}.getType();
            List<Book> loadedFavorites = gson.fromJson(jsonFavorites, type);
            if (loadedFavorites != null) {
                favoriteBookList.addAll(loadedFavorites);
            }
        }

        if (favoriteBookList.isEmpty()) {
            textViewNoFavorites.setVisibility(View.VISIBLE);
            recyclerViewFavorites.setVisibility(View.GONE);
        } else {
            textViewNoFavorites.setVisibility(View.GONE);
            recyclerViewFavorites.setVisibility(View.VISIBLE);
        }
        favoriteBookAdapter.notifyDataSetChanged();
    }
}